import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 * User: zm
 * Date: 2023/5/16
 */
public class Demo36 {
    static int N = 100010;
    static int[] p = new int[N];
    static int[] size = new int[N];
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        for(int i = 1; i <= n; i++){
            p[i] = i;
            size[i] = 1;
        }
        int res = 0;
        while(m -- > 0){
            String s = scan.next();
            if(s.equals("C")){
                int a = scan.nextInt();
                int b = scan.nextInt();
                if(find(a) == find(b))continue;
                size[find(b)] += size[find(a)];
                p[find(a)] = find(b);
            }else if(s.equals("Q1")){
                int a = scan.nextInt();
                int b = scan.nextInt();
                if(find(a) == find(b)) System.out.println("Yes");
                else System.out.println("No");
            }else{
                int a = scan.nextInt();
                System.out.println(size[find(a)]);
            }

        }
    }
    public static int find(int x){
        if(x == p[x])return x;
        return p[x] = find(p[x]);
    }
}
